const model = require('../models/vehicleModel'),
  errCode = require('../../conf/errorCode'),
  wrap = require('../helper/wrap'),
  conf = require('../../conf/const'),
  util = require('../helper/util'),
  moment = require('moment'),
  _ = require('lodash'),
  {parseIntPageSize} = require('./commonService');

const DATE_FORMAT = 'YYYY-MM-DD HH:mm';

exports.getVehicleBrandByCapital = function(capital, callback) {
  var tmp = util.firstKeyOfNull({capital}, ['capital']);
  if (tmp != null) {
    return callback({errNo: errCode.LESS_OF_DATA_ERROR, extraMsgs: tmp});
  }
  model.getVehicleBrandByCapital(capital, (err, _rel) => {
    if (err) {
      return callback(err);
    }
    let list = _rel.list,
      flag = 0,
      wrapRel = [];

    for (let i = 0; i < list.length; i++) {
      let az = '';
      for (let j = 0; j < wrapRel.length; j++) {
        if (wrapRel[j].name == list[i].brandName) {
          flag = 1;
          az = j;
          break;
        }
      }
      if (!flag) {
        let brandItem = {};
        brandItem.name = list[i].brandName;
        brandItem.url = list[i].brandLogoUrl;
        brandItem.modelList = [];
        brandItem.modelList.push(list[i]);
        wrapRel.push(brandItem);
      } else {
        wrapRel[az].modelList.push(list[i]);
        flag = 0;
      }
    }
    return callback(null, wrapRel);
  });
};

exports.getVehicleSpecByModelId = function(modelId, callback) {
  var tmp = util.firstKeyOfNull({modelId}, ['modelId']);
  if (tmp != null) {
    return callback({errNo: errCode.LESS_OF_DATA_ERROR, extraMsgs: tmp});
  }
  model.getVehicleSpecByModelId(modelId, (err, _rel) => {
    _rel.list.map(item => {
      item.updateAt = moment(item.updateAt).format(DATE_FORMAT);
      item.img = item.specImg ? item.specImg : item.modelImg;
      delete item.specImg;
      delete item.modelImg;
    });
    if (err) {
      return callback(err);
    } else {
      return callback(null, _rel);
    }
  });
};

exports.getVehicleSpecBySpecId = function(specId, callback) {
  var tmp = util.firstKeyOfNull({specId}, ['specId']);
  if (tmp != null) {
    return callback({errNo: errCode.LESS_OF_DATA_ERROR, extraMsgs: tmp});
  }
  model.getVehicleSpecBySpecId(specId, (err, _rel) => {
    if (err) {
      callback(err);
    }
    let specDetail = _rel[0];
    specDetail.img = specDetail.specImg || specDetail.modelImg;
    delete specDetail.specImg;
    delete specDetail.modelImg;
    return callback(null, specDetail);
  });
};

exports.updateSpecData = function(specId, data, callback) {
  var tmp = util.firstKeyOfNull({specId}, ['specId']);
  if (tmp != null) {
    return callback({errNo: errCode.LESS_OF_DATA_ERROR, extraMsgs: tmp});
  }
  model.updateSpecData(
    specId,
    wrap.changeData(data, wrap.names['spec'], wrap.names.front, wrap.names.end),
    (err, result) => {
      if (err) return callback(err);
      return callback(null, {
        affectData: result.affectedRows,
        updatedData: result.changedRows,
      });
    },
  );
};

exports.getCarType = function(callback) {
  model.getCarType(callback);
};

exports.getModelDetailBySpecId = function(modelId, callback) {
  var tmp = util.firstKeyOfNull({modelId}, ['modelId']);
  if (tmp != null) {
    return callback({errNo: errCode.LESS_OF_DATA_ERROR, extraMsgs: tmp});
  }
  model.getModelDetailBySpecId(modelId, callback);
};

exports.updateModelDetailByModelId = function(modelId, data, callback) {
  var tmp = util.firstKeyOfNull({modelId}, ['modelId']);
  if (tmp != null) {
    return callback({errNo: errCode.LESS_OF_DATA_ERROR, extraMsgs: tmp});
  }
  model.updateModelDetailByModelId(modelId, data, (err, result) => {
    if (err) return callback(err);
    return callback(null, {
      affectData: result.affectedRows,
      updatedData: result.changedRows,
    });
  });
};

exports.getBrandListByNamePrefix = function(namePrefix, callback) {
  model.getBrandListByNamePrefix(namePrefix, callback);
};

exports.addModel = function(
  {typeId, brandId, modelName, modelImg, released},
  callback,
) {
  var tmp = util.firstKeyOfNull(
    {typeId, brandId, modelName, modelImg},
    ['typeId', 'brandId', 'modelName', 'modelImg'],
  );
  if (tmp != null) {
    return callback({errNo: errCode.LESS_OF_DATA_ERROR, extraMsgs: tmp});
  }
  model.addModel(
    {typeId, brandId, modelName, modelImg, released},
    (err, result) => {
      if (err) return callback(err);
      return callback(null, {id: result.insertId});
    },
  );
};
